/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function getHttpObject(){
    var xmlHttp = false;
    try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            xmlHttp=false;
        }
    }
    if (!xmlHttp && typeof XMLHttpRequest != 'undefined'){
        xmlHttp = new XMLHttpRequest();
    }
    return xmlHttp;
}

function XMLwithXSTL(xmlFile, xsltFile,idLocation){
    var xmlDOM=new ActiveXObject ("Microsoft.XMLDOM");
    xmlDOM.async=false;
    xmlDOM.load(xmlFile);
    var xsltDOM=new ActiveXObject("Microsoft.XMLDOM");
    xsltDOM.async=false;
    xsltDOM.load(xsltFile);
    if (xmlDOM.parseError.errorCode!=0) {
        alert("Error: "+xmlDoc.parseError.reason);
    }else if (xsltDOM.parseError.errorCode!=0) {
        alert("Error: "+xmlDoc.parseError.reason);
    }else{
        var out=xmlDOM.transformNode(xsltDOM);
        document.getElementById(idLocation).innerHTML=out;

    }
}

function getValue(id){
    xmlHttp=getHttpObject();
    if(xmlHttp){
        var idProduct=document.getElementById(id).value;
        var proType=document.getElementById("proType"+id).value;
        var noSell=document.getElementById("noSell"+id).checked

        var noSellValue;
        if (noSell) {
            noSellValue=1;
        } else{
            noSellValue=0;
        }
        xmlHttp.open("POST","AdminController", true);

        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

        var url="btAction=editCheckbox&txtID="+idProduct+"&txtProType="+proType+"&txtSell="+noSellValue;

        xmlHttp.send(url);

        xmlHttp.onreadystatechange = function(){
            alert(xmlHttp.responseText);
        }
    }
}

function adminSearch(){
    xmlHttp=getHttpObject();
    if(xmlHttp){
        var productName=document.getElementById("txtSearch").value;

        xmlHttp.open("POST","AdminController", true);

        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

        var url="btAction=searchProduct&txtProductName="+productName;

        xmlHttp.send(url);

        xmlHttp.onreadystatechange=function(){
            if (xmlHttp.readyState==4) {
                if (xmlHttp.status==200) {
                    XMLwithXSTL("XML/resultProductSearch.xml", "xsl/Admin.xsl", "content");
                }
            }
        }

    }
}

function AddProduct(){
        XMLwithXSTL("XML/Catalogues.xml", "xsl/addProduct.xsl", "content")
//        document.getElementById("content").innerHTML=out;
}

//function showSearchResult(){
//    if(xmlHttp.readyState == 4){
//        if(xmlHttp.status == 200){
//            XMLwithXSTL("XML/resultProductSearch.xml", "xsl/Admin.xsl", "content")
////            document.getElementById("content").innerHTML=out;
//        }
//    }
//}

function editProductDetail(id){
    var idPro=document.getElementById(id).value;
    var namePro=document.getElementById("name"+id).value;
    var pricePro=document.getElementById("price"+id).value;
    var desPro=document.getElementById("des"+id).value;
    var imagePro=document.getElementById("image"+id).value;
    var catalogPro=document.getElementById("catalog"+id).value;
    var numberPro=document.getElementById("proNum"+id).value;
    var fromPro=document.getElementById("proFrom"+id).value;
    var brandPro=document.getElementById("brandName"+id).value;

    var xmlDOM=new ActiveXObject ("Microsoft.XMLDOM");
    //create the root node for newXML
    xmlDOM.async=false;
    xmlDOM.load("XML/Catalogues.xml");
    var xpathExpCata="//Catalogue";
    xmlDOM.setProperty("SelectionLanguage", "XPath");
    var resultCata=xmlDOM.selectNodes(xpathExpCata);
    var arrayCatalogs=new Array();
    for (var i = 0; i < resultCata.length; i++) {
        arrayCatalogs[i]=resultCata.item(i).selectSingleNode("catalogName").text;
    }
    //end getCatalog

    xmlDOM.async=false;
    xmlDOM.load("XML/Brands.xml");
    var xpathExpBra="//brand";
    xmlDOM.setProperty("SelectionLanguage", "XPath");
    var resultBra=xmlDOM.selectNodes(xpathExpBra);
    var arrayBrands=new Array();
    for (var j = 0; j < resultBra.length; j++) {
        arrayBrands[j]=resultBra.item(j).selectSingleNode("brandName").text;
    }
    //end getBrand
    
    var out = "<h1 style='margin-top: 20px; margin-left: 20px'>Edit Product Detail</h1>"
    + "<form id='formregister' method='POST' action='Product' enctype='multipart/form-data' onsubmit='return validateEditPro(this);'>"
    + "<table border='0'>"
    + "<tr>"
    + "<td id='text' style='font-size:18px'>ID</td>"
    + "<td>"
    + "<label>"
    + "<input name='txtID' disabled='disabled' type='text' style='width:50px' value='"+idPro+"' readonly/><br/>"
    + "<input name='txtID' type='hidden' id='txtID' style='width:50px' value='"+idPro+"' readonly/><br/>"
    + "<span id='idID' style='color:red'></span>"
    + "</label>"
    + "</td>"
    + "</tr>"
    + "<tr>"
    + "<td id='text' style='font-size:18px'>Product Name(*)</td>"
    + "<td>"
    + "<label>"
    + "<input name='txtProductname' type='text' id='txtProductname' style='width:500px' value='"+namePro+"' onchange='checkName(this.value)'/><br/>"
    + "<span id='idProductname' style='color:red'></span>"
    + "</label>"
    + "</td>"
    + "</tr>"
    + "<tr>"
    + "<td id='text' style='font-size:18px'>Sell Price(*)</td>"
    + "<td>"
    + "<label>"
    + "<input name='txtPrice' type='text' id='txtPrice' onchange='checkPrice(this.value)' value='"+pricePro+"' /><br/>"
    + "<span id='idPrice' style='color:red'></span>"
    + "</label>"
    + "</td>"
    + "</tr>"
    + "<tr>"
    + "<td id='text' style='font-size:18px'>Description(*)</td>"
    + "<td>"
    + "<label>"
    + "<textarea name='txtDescrip' rows='5' id='txtDescrip' onchange='checkDescrip(this.value)' style='width:500px'>"+""+desPro+""
    + "</textarea><br/>"
    + "<span id='idDescrip' style='color:red'></span>"
    + "</label>"
    + "</td>"
    + "</tr>"
    + "<tr>"
    + "<td id='text' style='font-size:18px'>Category</td>"
    + "<td>"
    + "<label>"
    + "<select name='txtCatelog' type='text' id='txtCatelog' style='width:150px'>";
    for (i = 0; i < arrayCatalogs.length; i++) {
        if (arrayCatalogs[i]==catalogPro) {
            out=out+"<option selected='selected'>"+arrayCatalogs[i]+"</option>";
        }else{
            out=out+"<option>"+arrayCatalogs[i]+"</option>";
        }
    }

    out=out+ "</select><br/>"
    + "<span id='idCatelog' style='color:red'></span>"
    + "</label>"
    + "</td>"
    + "</tr>"

    + "<tr>"    
    + "<td id='text' style='font-size:18px'>Brand</td>"
    + "<td>"
    + "<label>"
    + "<select name='txtBrand' type='text' id='txtBrand' style='width:150px'>";
    for (j = 0; j < arrayBrands.length; j++) {
        if (arrayBrands[j]==brandPro) {
            out=out+"<option selected='selected'>"+arrayBrands[j]+"</option>";
        }else{
            out=out+"<option>"+arrayBrands[j]+"</option>";
        }
    }

    out=out+ "</select><br/>"
    + "<span id='idBrand' style='color:red'></span>"
    + "</label>"
    + "</td>"
    + "</tr>"
    + "<tr>"

    + "<td id='text' style='font-size:18px'>Image</td>"
    + "<td>"
    + "<label>"
    + "<br/>"
    + "<img width='190px' height='200px' src='" +imagePro+ "'/><br/>"
    + "<input type='file' name='txtImage' value='' id='txtImage'/><br/>"
    + "<input type='hidden' name='txtImage2' value='"+imagePro+"' id='txtImage2'/><br/>"
    + "<span id='idImage' style='color:red'></span>"
    + "</label>"
    + "</td>"
    + "</tr>"
    + "<tr>"
    + "<td id='text' style='font-size:18px'>Quantity(*)</td>"
    + "<td>"
    + "<label>"
    + "<input name='txtNumber' type='text' id='txtNumber' onchange='checkNumber(this.value)' value='"+numberPro+"'/><br/>"
    + "<span id='idNumber' style='color:red'></span>"
    + "</label>"
    + "</td>"
    + "</tr>"
    + "<tr>"
    + "<td id='text' style='font-size:18px'>Country(*)</td>"
    + "<td>"
    + "<label>"
    + "<input name='txtFrom' type='text' id='txtFrom' onchange='checkFrom(this.value)' value='"+fromPro+"' /><br/>"
    + "<span id='idFrom' style='color:red'></span>"
    + "</label>"
    + "</td>"
    + "</tr>"
    + "<tr>"
    + "<td colspan='2'>"
    + "<input name='btAction' type='submit' id='btSubmit' value='Update' style='margin-left: 350px; padding: 10px'/>"
    + "</td>"
    + "</tr>"
    + "</table>"
    + "</form>"
    document.getElementById("content").innerHTML=out;
}
